/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.header;

import javax.sip.InvalidArgumentException;
import javax.sip.header.CSeqHeader;
import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class CSeqHeaderImpl extends HeaderBase implements CSeqHeader {
    private String method;
    private long seqNumber;

    private static final CSeqHeaderParser parser = new CSeqHeaderParser();

    public CSeqHeaderImpl() {
        super(CSeqHeader.NAME);
    }

    CSeqHeaderImpl(byte[] data, int start, int end) {
        super(CSeqHeader.NAME, data, start, end);
    }

    public Object clone() {
        final CSeqHeaderImpl header = new CSeqHeaderImpl();
        header.cloneBase(this);
        if (parsed) {
            header.method = this.method;
        }
        return header;
    }

    protected HeaderParser getParser() {
        return parser;
    }

    protected String getValueAsString() {
        return Long.toString(this.seqNumber) + ' ' + this.method;
    }

    /**
     * Sets the method of CSeqHeader.
     *
     * @param method the method of the Request of this CSeqHeader.
     * @throws java.text.ParseException which signals that an error has been reached
     *                                  unexpectedly while parsing the method value.
     */
    public void setMethod(String method) throws ParseException {
        parse();
        this.method = method;
        invalidateHeaderData();
    }

    /**
     * Gets the method of CSeqHeader.
     *
     * @return method of CSeqHeader
     */
    public String getMethod() {
        parse();
        return method;
    }

    /**
     * Sets the sequence number value of the CSeqHeader. The sequence number
     * MUST be expressible as a 32-bit unsigned integer and MUST be less than
     * 2**32 -1
     *
     * @param sequenceNumber the new sequence number of this CSeqHeader
     * @throws javax.sip.InvalidArgumentException
     *          if supplied value is less than zero.
     * @since v1.1
     * @deprecated This method is replaced with {@link #setSeqNumber(long)} with
     *             type long.
     */
    public void setSequenceNumber(int sequenceNumber) throws InvalidArgumentException {
        parse();
        this.seqNumber = sequenceNumber;
        invalidateHeaderData();
    }

    /**
     * Gets the sequence number of this CSeqHeader.
     *
     * @return sequence number of the CSeqHeader
     * @deprecated This method is replaced with {@link #getSeqNumber()} with
     *             type long.
     */
    public int getSequenceNumber() {
        parse();
        return (int) seqNumber;
    }

    /**
     * Sets the sequence number value of the CSeqHeader.
     *
     * @param sequenceNumber - the new sequence number of this CSeqHeader
     * @throws javax.sip.InvalidArgumentException
     *          if supplied value is less than zero.
     */
    public void setSeqNumber(long sequenceNumber) throws InvalidArgumentException {
        parse();
        this.seqNumber = sequenceNumber;
        invalidateHeaderData();
    }

    /**
     * Gets the sequence number of this CSeqHeader.
     *
     * @return sequence number of the CSeqHeader
     */
    public long getSeqNumber() {
        parse();
        return this.seqNumber;
    }
}
/*
 * $Log$
 */